﻿@model Xms.Web.Customize.Models.EditDuplicateRuleModel

<div class="panel panel-default">
    <div class="panel-heading">
        <h3 class="panel-title">
            <a data-toggle="collapse"
               href="#collapseTwo">
                <strong>@app.PrivilegeTree?.LastOrDefault().DisplayName - @Model.EntityMeta.LocalizedName</strong>
            </a>
        </h3>
    </div>
    <div id="collapseTwo" class="panel-collapse collapse in">
        <div class="panel-body">
            <form action="/@app.OrganizationUniqueName/customize/@app.ControllerName/@app.ActionName" method="post" id="editform" class="form-horizontal" role="form">
                @Html.AntiForgeryToken()
                @Html.ValidationSummary()
                @Html.HiddenFor(x => x.EntityId)
                @Html.HiddenFor(x => x.DuplicateRuleId)
                <div class="form-group col-sm-12">
                    <label class="col-sm-2 control-label">实体</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control input-sm" value="@Model.EntityMeta.LocalizedName" disabled />
                    </div>
                </div>
                <div class="form-group col-sm-12">
                    @Html.LabelFor(x => x.Name, "名称", new { @class = "col-sm-2 control-label" })
                    <div class="col-sm-10">
                        @Html.TextBoxFor(x => x.Name, new { @class = "form-control required" })
                    </div>
                </div>
                <div class="form-group col-sm-12">
                    @Html.LabelFor(x => x.Description, "描述", new { @class = "col-sm-2 control-label" })
                    <div class="col-sm-10">
                        @Html.TextAreaFor(x => x.Description, new { @class = "form-control", @rows = 3 })
                    </div>
                </div>
                <div class="form-group col-sm-12">
                    @Html.LabelFor(x => x.Intercepted, "拦截", new { @class = "col-sm-2 control-label" })<!--"状态"-->
                    <div class="col-sm-10">
                        <label class="checkbox-inline">
                            @Html.RadioButtonFor(x => x.Intercepted, true, new { @class = "required" }) @app.T["yes"]
                        </label>
                        <label class="checkbox-inline">
                            @Html.RadioButtonFor(x => x.Intercepted, false, new { @class = "required" }) @app.T["no"]
                        </label>
                    </div>
                </div>
                <div class="form-group col-sm-12">
                    @Html.LabelFor(x => x.StateCode, "状态", new { @class = "col-sm-2 control-label" })
                    <div class="col-sm-10">
                        <label class="checkbox-inline">
                            @Html.RadioButtonFor(x => x.StateCode, Xms.Core.RecordState.Enabled, new { @class = "required" }) 可用
                        </label>
                        <label class="checkbox-inline">
                            @Html.RadioButtonFor(x => x.StateCode, Xms.Core.RecordState.Disabled, new { @class = "required" }) 禁用
                        </label>
                    </div>
                </div>
                <div class="form-group col-sm-12">
                    <label class="col-sm-2 control-label" for="optionset-picklist">条件</label>
                    <div class="col-sm-10">
                        <div id="optionset-area">
                            <div class="btn-group">
                                <button type="button" class="btn btn-primary btn-xs" onclick="addOption('optionset-picklist')">
                                    <span class="glyphicon glyphicon-plus-sign"></span> 增加
                                </button>
                                <button type="button" class="btn btn-default btn-xs" onclick="clearOption('optionset-picklist')">
                                    <span class="glyphicon glyphicon-trash"></span> 清空
                                </button>
                            </div>
                            <div class="text-muted pull-right">字段不能重复选择</div>
                            <table id="optionset-picklist" class="table">
                                <thead>
                                    <tr>
                                        <th>字段</th>
                                        <th>忽略空值</th>
                                        <th>区分大小写</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach (var item in Model.Conditions)
                                    {
                                        <tr class="row_clone">
                                            <td>
                                                <input type="text" name="attributeid" value="@item.AttributeId" />
                                                <input type="hidden" name="detailid" value="@item.DuplicateRuleConditionId" />
                                            </td>
                                            <td>
                                                <input type="checkbox" onclick="$(this).next().val($(this).prop('checked'))" @(item.IgnoreNullValues ? "checked" : "") />
                                                <input name="ignorenullvalues" type="hidden" value="@(item.IgnoreNullValues.ToString())" />
                                            </td>
                                            <td>
                                                <input type="checkbox" onclick="$(this).next().val($(this).prop('checked'))" @(item.IsCaseSensitive ? "checked" : "") />
                                                <input name="iscasesensitive" type="hidden" value="@(item.IsCaseSensitive.ToString())" />
                                            </td>
                                            <td>
                                                <button type="button" class="btn btn-warning btn-xs" onclick="removeOption('optionset-picklist', this)">
                                                    <span class="glyphicon glyphicon-trash"></span>
                                                </button>
                                            </td>
                                        </tr>
                                    }
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                <div class="form-group col-sm-12 text-center" id="form-buttons">
                    <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-saved"></span> 保存</button>
                    <button type="reset" class="btn btn-default"><span class="glyphicon glyphicon-refresh"></span> 重置</button>
                </div>
            </form>
        </div>
    </div>
</div>
@section Scripts {
    <script src="/content/js/xms.metadata.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="/content/js/jquery.form.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="/content/js/jquery-validate/jquery.validate.min.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="/content/js/jquery-validate/localization/messages_zh.min.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script>
        var AttributeMetaDatas = [];
        $(function () {
            //表单验证
            Xms.Web.Form($("#editform"), function (response) {
                Xms.Web.Alert(response.IsSuccess, response.Content, function () {
                    reloadSetAttribute();
                }, function () {
                    reloadSetAttribute();
                });

            });

            loadAttribute();
        });
        function loadAttribute() {
            Xms.Schema.GetAttributes({ getall: true, entityid: $('#EntityId').val() }, function (response) {
                AttributeMetaDatas = [];
                $(response).each(function (i, n) {
                    var attr = {};
                    attr.id = n.attributeid;
                    attr.name = n.name;
                    attr.text = n.localizedname;
                    attr.value = n.attributeid;

                    AttributeMetaDatas.push(attr);
                });
                setAttributeOptions();
            });
        }
        function setAttributeOptions() {
            $('#optionset-picklist').find('input[name=attributeid]:not(.hide)').each(function () {
                $(this).removeAttr('data-picklistinit').removeAttr('data-instance');
                if ($(this).next('select').length > 0) {
                    $(this).next('select').remove()
                }
                var value = $(this).val();
                console.log(value)
                $(this).picklist({
                    isDefault: true,
                    items: AttributeMetaDatas
                });
            });
        }
        function reloadSetAttribute() {
            $('#optionset-picklist').find('input[name=attributeid]').each(function () {
                var seler = $(this).next(), val = $(this).attr('data-value');
                if (val != "") {
                    seler.val(val);
                }
            });
        }
        //增加选项
        function addOption(id) {
            var $target = $("#" + id);
            var newRow = $target.find('tr:last').clone();
            newRow.find('input[type=text]').val('');
            newRow.find('input[type=hidden]').val(Xms.Utility.Guid.EmptyGuid.ToString());
            newRow.find('input[type=checkbox]').prop('checked', false).next().val(false);
            $target.append(newRow);
            $target.find('select:last').find('option:not(.hide)').eq(1).prop('selected', true).trigger('change');
            OptionState();
            setAttributeOptions();
            return newRow;
        }
        //删除选项
        function removeOption(id, row) {
            var $target = $("#" + id);
            if ($(row).parents('tr').siblings().length > 0) {
                $(row).parents('tr').remove();
            }
            else {
                var newRow = $(row).parents('tr');
                $target.find('tbody').empty();
                addOption(id, newRow);
                setAttributeOptions();
            }
        }
        //清空选项
        function clearOption(id,isSetOptions) {
            var $target = $("#" + id);
            $target.find('tbody').find('tr:gt(0)').remove();
            var newRow = addOption(id, newRow).clone(true);
            newRow.find('input[name="attributeid"]').removeClass('hide');
            $target.find('tbody').empty();
            $target.find('tbody').append(newRow);
            if (!isSetOptions) {
                setAttributeOptions();
            }
        }
         function OptionState() {
            $('#optionset-picklist option').removeClass('show');
            $('#optionset-picklist select').each(function (i, n) {
                var val = $(n).find('option:selected').val();
                $(n).find('option[value=""]').addClass('hide');
                $(n).parents('tr').siblings('tr').find('option[value="' + val + '"]').addClass('hide');
            });
        }
    </script>
}